package com.example.demo.bean;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ServletConTest1 {
    public class ServeletContext extends HttpServlet {
        private ServletConfig config;
        private ServletContext context;

        @Override
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
            this.config = config;
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            context = this.config.getServletContext();
            String url = this.context.getInitParameter("url");
            String username = this.context.getInitParameter("username");
            String password = this.context.getInitParameter("password");
            response.setCharacterEncoding("UTF-8");//
            response.setContentType("text/html");//
            PrintWriter out = response.getWriter();//
            //以下为输出的html正文数据
            out.println("<HTML>");
            out.println("<HEAD><TITLE>ServletContext</TITLE></HEAD>");
            out.println("<BODY>");
            out.println("获取连接数据库的初始化参数:" + "<br/>");
            out.println("url:" + url + "<br/>");
            out.println("username" + username + "<br/>");
            out.println("password" + password + "<br/>");
            out.println("</BODY>");
            out.println("</HTML>");
            out.flush();
            out.close();
        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    }
}
